<?php 
 function num_page() 
  { 
    global $n_record;//lay bien toan cuc 
    global $p; 
    if($n_record%$p==0) 
    { 
      $n_page=$n_record/$p; 
      return $n_page; 
    } 
    else 
    { 
      $n_page=($n_record-($n_record%$p))/$p+1; 
      return $n_page; 
    } 
  } 
  
   function view_page($link) 
  { 
   $begin=1;
   $sotranght=7;
   $totalpage=num_page();
   if( $totalpage>1){
		$n=$_GET['n'];
		if($n=="")
		{
		$n=1;
		}
		global $n_record; 
		//kiem tra n= first thi day ve dau trang
		if($n=='first')	{echo "Đầu | ";}else{echo "<a href='$link&n=first'>Đầu</a> | ";}
		/////////////////////////////////////////
		//Vong lap in so trang ra man hinh
		
		//for($i=1;$i<=num_page();$i++)
		$tinhtoan=$begin+$sotranght;
		if($begin<=$n-3){$begin=$n-3;
		$tinhtoan=$begin+$sotranght;
		if($totalpage<$tinhtoan){$tinhtoan=$totalpage;$begin=$tinhtoan-$sotranght;if($begin<1){$begin=1;}}
		}else{
			if($totalpage<$tinhtoan){$tinhtoan=$totalpage;$begin=$tinhtoan-$sotranght;if($begin<1){$begin=1;}}
			else{$tinhtoan=$sotranght;}}
		
		$i=$begin;		
		for($i;$i<=$tinhtoan;$i++) 
		{ 
				if($i==$n)
				{
					echo $i." | ";
				}
				else
				{
					echo "<a href='$link&n=".$i."'>".$i."</a> | "; 
				}
		} 
		//kiem tra n= last thi day ve cuoi trang
		if($n=='last'){	echo "Cuối  ";	}else{echo "<a href='$link&n=last'>Cuối</a> "; 	}
		/////////////////////////////////////////
	}	
  } 
  /////////////////////////////BAT CAC LOI PHAN TRANG///////////////////////////////////
	//Neu So trang lon hon tong so trang thi day be trang chuoi cung
	if($n>num_page())
	{
		$n=num_page();
	}
	/////////////////////	
	if(is_numeric($n)){}
	else{
	///////////kiem tra neu n khong phai la so thi...
	if($n=="" || $n=="first"){$n=1;}else{
	   if($n=="last"){$n=num_page();}else
	   		{
			$n=1;
			}
		}
	}
	$s=($n-1)*$p;
?>